﻿using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using Maticsoft.BLL;
using Maticsoft.Model;
namespace FineUI.Examples
{
    public partial class teacher_release : System.Web.UI.Page
    {
        protected void ExistsPermiss(string P_Id, string P_Status)
        {
           


            Maticsoft.BLL.Permissions PM = new Maticsoft.BLL.Permissions();
            if (!PM.ExistsPermiss(P_Id, P_Status))
            {
                Response.Redirect("~/user_no_permission.aspx");
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("~/login.aspx");
            }
            ExistsPermiss("T003", "已授权");
            BindGrid();
        }
        public DataSet BindGrid()
        {
            string currentT_Id = Session["UserName"].ToString();
            Maticsoft.BLL.ReportTemplate RT = new Maticsoft.BLL.ReportTemplate();
            DataSet RtDate = RT.GetList("ReportTemplate.TC_Id=TeachingClass.TC_Id and TeachingClass.T_Id=Teacher.T_Id and Teacher.T_Id= '" + currentT_Id + "'");
            //教学班没有排课

            DataView view1 = RtDate.Tables[0].DefaultView;
            Grid1.DataSource = view1;
            Grid1.DataBind();
            return RtDate;


        }

        protected void DocDownLoad(string current_R_Id)
        {
            string currentUser=Session["UserName"].ToString();
            Maticsoft.BLL.ReportTemplate documentation= new Maticsoft.BLL.ReportTemplate();
            Maticsoft.Model.ReportTemplate documentInform = documentation.GetModel(current_R_Id);
            string fileRpath = Server.MapPath(@"~/document/teacher/") + documentInform.R_Path;
          
            Response.ClearHeaders();
            Response.Clear();
            Response.Expires = 0;
            Response.Buffer = true;
            Response.AddHeader("Accept-Language", "zh-tw");
            string name = documentInform.R_Formerly.ToString();
            System.IO.FileStream files = new FileStream(fileRpath, FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] byteFile = new byte[files.Length];
           
            files.Read(byteFile, 0, byteFile.Length);
            files.Close();
            Response.ContentType = "application/octet-stream;charset=gbk";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
            
            Response.BinaryWrite(byteFile);
            Response.Flush();
            Response.End();


        }

        protected void DocDelete(string current_R_Id)
        {
            string currentUser = Session["UserName"].ToString();
            Maticsoft.BLL.ReportTemplate DocTemplate = new Maticsoft.BLL.ReportTemplate();
            Maticsoft.Model.ReportTemplate DocTemplateInfom = DocTemplate.GetModel(current_R_Id);

            string filepath = Server.MapPath(@"~/document/teacher/") + DocTemplateInfom.R_Path;
            if(File.Exists(filepath))
            {
                File.Delete(filepath);
            }
            DocTemplate.Delete(current_R_Id);


        
        }
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "ActionDownLoad")//下载
            {
                int key = e.RowIndex;
                string CurrentID = BindGrid().Tables[0].Rows[key]["R_Id"].ToString();
                DocDownLoad(CurrentID);    
                    //DataBind().Table[0].Row[key]["R_Id"].ToString();
            }
            if (e.CommandName == "ActionDelete")
            {
                int key = e.RowIndex;
                string CurrentID = BindGrid().Tables[0].Rows[key]["R_Id"].ToString();
                DocDelete(CurrentID);
                BindGrid();

            }
            if (e.CommandName == "ActionUpdate")
            {
                int key = e.RowIndex;
                string CurrentID = BindGrid().Tables[0].Rows[key]["R_Id"].ToString();
                string CurrentSTime = BindGrid().Tables[0].Rows[key]["R_STime"].ToString();
                string CurrentETime = BindGrid().Tables[0].Rows[key]["R_ETime"].ToString();
                Session["R_STime"] = CurrentSTime;
                Session["R_ETime"] = CurrentETime;
                DocDelete(CurrentID);
                Response.Redirect("~/teacher_update.aspx?a=1");
            }




        }

       /* protected void search_btn_Click(object sender, EventArgs e)
        {
            string currentFormerly = search_box.Text.ToString();

            Maticsoft.BLL.ReportTemplate RT = new Maticsoft.BLL.ReportTemplate();
            DataSet RtDate = RT.GetList("ReportTemplate.T_Id=Teacher.T_Id and ReportTemplate.R_Formerly like '%" + currentFormerly + "%' order by R_STime desc");
            DataView view1 = RtDate.Tables[0].DefaultView;
            Grid1.DataSource = view1;
            Grid1.DataBind();
            

        }*/

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
    }
}